package com.chenjiahao.kaiyanwan.ui.discovery

import android.graphics.Color
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.*
import com.bumptech.glide.Glide
import com.chenjiahao.kaiyanwan.BUNDLE_CATEGORY_DATA
import com.chenjiahao.kaiyanwan.R
import com.chenjiahao.kaiyanwan.base.BaseActivity
import com.chenjiahao.kaiyanwan.logic.exception.ExceptionHandle
import com.chenjiahao.kaiyanwan.logic.util.StatusBarUtil
import com.hazz.kotlinmvp.mvp.model.bean.CategoryBean
import kotlinx.android.synthetic.main.activity_category_detail.*
import java.lang.Exception

class CategoryDetailActivity : BaseActivity() {

    private var categoryData: CategoryBean? = null

    private val mAdapter by lazy { CategoryDetailAdapter(this, viewModel.itemList, R.layout.item_category_detail) }

    private val viewModel by lazy { ViewModelProvider(this).get(CategoryDetailViewModel::class.java) }

    override fun layoutId()= R.layout.activity_category_detail

    private var loadingMore=false

    override fun initData() {
        categoryData = intent.getSerializableExtra(BUNDLE_CATEGORY_DATA) as CategoryBean?
    }

    override fun initView() {
        //状态栏透明和间距处理
        StatusBarUtil.darkMode(this)
        StatusBarUtil.setPaddingSmart(this, toolbar)
        setSupportActionBar(toolbar)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        toolbar.setNavigationOnClickListener { finish() }
        // 加载headerImage
        Glide.with(this)
            .load(categoryData?.headerImage)
            .placeholder(R.color.color_darker_gray)
            .into(imageView)

        tv_category_desc.text ="#${categoryData?.description}#"

        collapsing_toolbar_layout.title = categoryData?.name
        collapsing_toolbar_layout.setExpandedTitleColor(Color.WHITE)//设置还没收缩时状态下字体颜色
        collapsing_toolbar_layout.setCollapsedTitleTextColor(Color.BLACK)//设置收缩后Toolbar上字体的颜色
        mRecyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerView.adapter = mAdapter
        mRecyclerView.addOnScrollListener(object : OnScrollListener() {
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                val itemCount = (mRecyclerView.layoutManager as LinearLayoutManager).itemCount
                val lastVisibleItem = (mRecyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
                if (!loadingMore && lastVisibleItem == (itemCount - 1)) {
                    loadingMore = true
                    //加载更多
                    viewModel.requestMoreData()
                }
            }
        })

    }

    override fun start() {
        categoryData?.id?.let { viewModel.requestCategoryDetailData(it) }
        viewModel.categoryDetailData.observe(this, Observer {
            val categoryDetailData=it.getOrNull()
            if(categoryDetailData!=null){
                viewModel.itemList=categoryDetailData.itemList
                viewModel.categoryDetailNextPageUrl=categoryDetailData.nextPageUrl
                mAdapter.addData(viewModel.itemList)
            }else{
                showError(ExceptionHandle.handleException(it.exceptionOrNull() as Exception), ExceptionHandle.errorCode)
            }
        })
        viewModel.getIssueData.observe(this, Observer {
            val categoryDetailData=it.getOrNull()
            loadingMore=false
            if(categoryDetailData!=null){
                viewModel.categoryDetailNextPageUrl=categoryDetailData.nextPageUrl
                viewModel.itemList=categoryDetailData.itemList
                mAdapter.addData(viewModel.itemList)
            }else{
                showError(ExceptionHandle.handleException(it.exceptionOrNull() as Exception), ExceptionHandle.errorCode)
            }
        })
    }


}
